﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using Husb.Data;
using Husb.DataUtil;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using CentralOffice.Task.Data;

using ApplicationTable = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsDataTable;
using ApplicationRow = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsRow;

namespace CentralOffice.Task.DataAccess
{
    public class ApplicationAdapter : DataAccessAdapter<CentralOffice.Task.Data.ApplicationDataSet, ApplicationTable, ApplicationRow>
    {
        private const string CompareApplicationCodeStoredProc = " SELECT COUNT(*) FROM Tasks.Applications WHERE ApplicationCode = @ApplicationCode";//  Tasks.Applications_CompareApplicationCode";
        private const string GetApplicationCodeStoredProc = " SELECT MAX(ApplicationCode) FROM Tasks.Applications WHERE ";
        private const string DeleteBatchApplicationStoredProc = " DELETE FROM Tasks.Applications WHERE Id IN ({0}) ";
        private const string UpdateRecoverApplicationStoredProc = " UPDATE Tasks.Applications SET IsDeleted = 0 WHERE Id IN ({0}) ";

        public ApplicationAdapter()
        {
            this.TableName = "Applications";

            GetAllStoredProc = "Tasks.Applications_SelectApplicationsAll";
            InsertStoredProc = "Tasks.Applications_InsertApplication";
            UpdateStoredProc = "Tasks.Applications_UpdateApplication";
            DeleteStoredProc = "Tasks.Applications_DeleteApplication";

            GetByIdStoredProc = "Tasks.Applications_SelectApplication";
            GetByNameStoredProc = "";
            GetPagedStoredProc = "Tasks.Applications_SelectApplicationsPaged";
            GetRowCountStoredProc = "Tasks.Applications_SelectApplicationsRowCount";
            //FindStoredProc = "Tasks.Tasks_SelectTasksDynamic";

            LogicalDeleteStoredProc = "Tasks.Applications_DeleteApplicationLogical";
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApplicationCode", DbType.AnsiString, "ApplicationCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "Name", DbType.AnsiString, "Name", DataRowVersion.Current);

            db.AddInParameter(cmd, "ProcessId", DbType.Guid, "ProcessId", DataRowVersion.Current);
            
            db.AddInParameter(cmd, "Category", DbType.Int32, "Category", DataRowVersion.Current);
            db.AddInParameter(cmd, "UrgencyDegree", DbType.Int32, "UrgencyDegree", DataRowVersion.Current);
            db.AddInParameter(cmd, "DateOfApplication", DbType.DateTime, "DateOfApplication", DataRowVersion.Current);
            db.AddInParameter(cmd, "Applicant", DbType.AnsiString, "Applicant", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovalContent", DbType.AnsiString, "ApprovalContent", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApplicationExecutor", DbType.AnsiString, "ApplicationExecutor", DataRowVersion.Current);
            db.AddInParameter(cmd, "ExecutiveDescription", DbType.String, "ExecutiveDescription", DataRowVersion.Current);
            db.AddInParameter(cmd, "Status", DbType.Int32, "Status", DataRowVersion.Current);
            db.AddInParameter(cmd, "Attachments", DbType.String, "Attachments", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsDeleted", DbType.Boolean, "IsDeleted", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsActive", DbType.Boolean, "IsActive", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, "CreatedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedBy", DbType.Guid, "CreatedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ModifiedTime", DbType.DateTime, "ModifiedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "LastModifiedBy", DbType.Guid, "LastModifiedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "Version", DbType.Int32, "Version", DataRowVersion.Current);
            db.AddInParameter(cmd, "Description", DbType.AnsiString, "Description", DataRowVersion.Current);
        }

        public int CompareApplicationCode(string applicationCode)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("ApplicationCode", DbType.AnsiString, applicationCode));
            //ApprovalDataSet ds = Husb.DataUtil.DataAccessUtil.GetDataSet<ApprovalDataSet>(SelectRejectionByApplicationId, parameters, this.TableName, false, false, null);
            //ApprovalTable dt = ds.Approvals;
            object o = Husb.DataUtil.DataAccessUtil.GetValue(CompareApplicationCodeStoredProc, parameters, false);

            if (o != null)
            {
                return (int)o;
            }

            return 0;

            // startDateName + " >=  '" + ((DateTime)row[startDateName]).ToString("yyyy-MM-dd") + " 00:00:00.000' AND  " + startDateName + " < '" + ((DateTime)row[endDataName]).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00.000'";
        }

        public string GetMaxApplicationCode(DateTime dateOfApplication)
        {
            string where = DataAccessUtil.GetPartWhereCondition("DateOfApplication", dateOfApplication, dateOfApplication);
            object o = Husb.DataUtil.DataAccessUtil.GetValue(GetApplicationCodeStoredProc + DataAccessUtil.TrimCriteria(where), null, false);

            if (o != null && o != DBNull.Value)
            {
                return (string)o;
            }

            return "";

            // startDateName + " >=  '" + ((DateTime)row[startDateName]).ToString("yyyy-MM-dd") + " 00:00:00.000' AND  " + startDateName + " < '" + ((DateTime)row[endDataName]).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00.000'";
        }

        //
        public int DeleteBatch(string ids)
        {
            string sql = String.Format(DeleteBatchApplicationStoredProc, ids);
            int cnt = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(sql, null, false,false, null);
            return cnt;
        }

        public int RecoverBatch(string ids)
        {
            string sql = String.Format(UpdateRecoverApplicationStoredProc, ids);
            int cnt = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(sql, null, false,false, null);
            return cnt;
        }

    }
}
